package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.pojo.annotation.MyLog;
import com.tgu.home_action.service.CaseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "案件管理")
@RestController
@RequestMapping("/case")
public class CaseController {
    @Autowired
    private CaseService caseService;

    @MyLog(actionId = 9, role = "family", type = 2)
    @ApiOperation("发布案件（只有家属可以操作）")
    @PreAuthorize("hasRole('family')")
    @PostMapping
    public ResponseData<Object> publishCase(@Validated(Case.insert.class) @RequestBody Case newCase, @ApiIgnore @ModelAttribute("familyUser") Family family) {
        return caseService.publishCase(newCase, family);
    }

    @MyLog(actionId = 9, role = "family", type = 3)
    @ApiOperation("修改案件（只有家属可以操作,id必须提供，经纬度必须同时提供）")
    @PreAuthorize("hasRole('family')")
    @PutMapping
    public ResponseData<Object> updateCase(@RequestBody Case nowCase, @ApiIgnore @ModelAttribute("familyUser") Family family) {
        return caseService.updateCase(nowCase, family);
    }

    @MyLog(actionId = 9, role = "family", type = 4)
    @ApiOperation("结束案件（只有家属可操作，state为2表示已找到，state为4时表示已取消）")
    @PreAuthorize("hasRole('family')")
    @PostMapping("/cancel")
    public ResponseData<Object> cancelCase(@ApiJsonObject(name = "CancelCase", value = {
            @ApiJsonProperty(key = "caseId", description = "案件ID(int)"),
            @ApiJsonProperty(key = "state", description = "结束案件状态")
    }) @RequestBody Map<String, Object> param, @ModelAttribute("familyUser") Family family) {
        Integer caseId = Integer.valueOf((String)param.get("caseId"));
        short state = Integer.valueOf((String)param.get("state")).shortValue();
        return caseService.endCase(caseId, state, family);
    }

    @ApiOperation("查询所有案件信息（可以根据id,familyId,oldManId,state,province,city,district筛选）")
    @GetMapping
    public ResponseData<List<Case>> getCases(Case caseInformation) {
        return caseService.getCases(caseInformation);
    }

    @ApiOperation("查询案件数量")
    @GetMapping("/number")
    public ResponseData<Integer> getCaseNumber() {
        return caseService.getCaseNumber();
    }

    @ApiOperation("查询正在进行中的案件（只有志愿者可操作）")
    @PreAuthorize("hasRole('volunteer')")
    @GetMapping("/ing")
    public ResponseData<List<Case>> getIngCase(@ApiParam("经度") @RequestParam Double longitude, @ApiParam("纬度") @RequestParam Double latitude,
                                               @ApiParam("时差（单位小时）") @RequestParam(required = false) Integer timeDiff, @ApiParam("距离（单位米）") @RequestParam(required = false) Integer distance, @ApiIgnore @ModelAttribute("volunteer") Volunteer volunteer) {
        return caseService.getIngCase(longitude, latitude, timeDiff, distance, volunteer);
    }

    @MyLog(actionId = 10, type = 2, role = "volunteer")
    @ApiOperation("接受/拒绝案件（state：1为接受，2为拒绝,3为拒绝）")
    @ApiResponses({
            @ApiResponse(message = "该案件不存在", code = 132),
            @ApiResponse(message = "请勿重复操作", code = 116)
    })
    @PreAuthorize("hasRole('volunteer')")
    @PostMapping("/volunteer")
    public ResponseData<Object> responseCase(@Valid @RequestBody VolunteerCase volunteerCase, @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteer) {
        return caseService.responseCase(volunteerCase, volunteer);
    }

    @ApiOperation("查询志愿者参与的所有案件")
    @GetMapping("/volunteer/all")
    public ResponseData<List<Case>> getVolunteerCases(@RequestParam Integer volunteerId) {
        return caseService.getVolunteerCases(volunteerId);
    }

    @ApiOperation("查询志愿者正在参与的案件")
    @GetMapping("/volunteer/ing")
    public ResponseData<List<Case>> getVolunteerIngCases(@RequestParam Integer volunteerId) {
        return caseService.getVolunteerIngCases(volunteerId);
    }

    @ApiOperation("查询本人正在执行的任务（只有志愿者可以操作）")
    @PreAuthorize("hasRole('volunteer')")
    @GetMapping("/volunteer/my")
    public ResponseData<List<Case>> getMyIngCases(@ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteer) {
        return caseService.getMyIngCases(volunteer);
    }

    @ApiOperation("查询正在进行的未反馈的案件（只有志愿者可操作）")
    @PreAuthorize("hasRole('volunteer')")
    @GetMapping("/volunteer/new")
    public ResponseData<List<Case>> getNewCases(@ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteer) {
        return caseService.getNewCases(volunteer);
    }

    @ApiOperation("查询案件参与的志愿者")
    @GetMapping("/volunteer")
    public ResponseData<List<Volunteer>> getVolunteers(@RequestParam Integer caseId) {
        return caseService.getVolunteers(caseId);
    }


    @ApiOperation("家属查询自己的所有案件")
    @GetMapping("/family/all")
    @PreAuthorize("hasRole('family')")
    public ResponseData<List<Case>> getFamilyAllCase(@ApiIgnore @ModelAttribute("familyUser") Family family) {
        return caseService.getFamilyAllCase(family);
    }

    @ApiOperation("家属查询自己正在进行的案件")
    @GetMapping("/family/ing")
    @PreAuthorize("hasRole('family')")
    public ResponseData<List<Case>> getFamilyIngCase(@ApiIgnore @ModelAttribute("familyUser") Family family) {
        return caseService.getFamilyIngCase(family);
    }

    @ApiOperation("通过id模糊搜索")
    @GetMapping("/like/id")
    public ResponseData<List<Case>> getCaseByLikeId(@RequestParam String id){
        return caseService.getCaseByLikeId(id);
    }


}
